package com.ymk.cmd;


import com.ymk.base.cmd.ACmd;
import com.ymk.base.cmd.CmdSign;
import com.ymk.base.exception.MallException;
import com.ymk.base.utlis.ParamsBuilder;
import com.ymk.bo.goods.GoodsCartBO;
import com.ymk.core.code.cmd.GoodsCmdCode;
import com.ymk.core.code.cmd.RbacCmdCode;
import com.ymk.core.code.req.ReqCode;
import com.ymk.bo.rbac.RbacRoleBO;
import com.ymk.bo.rbac.RbacRolePermBO;
import com.ymk.core.constants.SysConstants;
import com.ymk.service.IRbacRolePermService;
import com.ymk.service.IRbacRoleService;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

@Service
public class RbacRolePatchCmd extends ACmd {

    @Override
    public CmdSign executeSop(HttpServletRequest request, CmdSign sign) throws Exception {
        CmdSign respSign = new CmdSign(sign);
        RbacRoleBO rbacRoleBO = super.getData(sign, RbacRoleBO.class);
        if (ReqCode.UPDATE_STATUS.equals(sign.getReqCode())){
            this.updateStatus(rbacRoleBO);
        }else {
            this.rbacRoleService.update(rbacRoleBO);
            RbacRolePermBO rbacRolePermBO = new RbacRolePermBO();
            rbacRolePermBO.setRoleId(rbacRoleBO.getId());
            this.rbacRolePermService.del(rbacRolePermBO);

            ParamsBuilder paramsBuilder = ParamsBuilder.newBuild()
                    .addParam("id", rbacRoleBO.getId());
            RbacRoleBO dbRbacRoleBO = this.rbacRoleService.get(paramsBuilder.build());

            rbacRoleBO.getPermIdList().forEach(permId -> {
                RbacRolePermBO addRbacRolePermBO = new RbacRolePermBO();
                addRbacRolePermBO.setRoleId(rbacRoleBO.getId());
                addRbacRolePermBO.setPermId(permId);
                addRbacRolePermBO.setMchId(dbRbacRoleBO.getMchId());
                this.rbacRolePermService.post(addRbacRolePermBO);
            });
        }
        return respSign;
    }

    @Override
    public CmdSign executeMop(HttpServletRequest request, CmdSign sign) throws Exception {
        CmdSign respSign = new CmdSign(sign);
        RbacRoleBO rbacRoleBO = super.getData(sign, RbacRoleBO.class);

        // 获取角色数据
        ParamsBuilder paramsBuilder = ParamsBuilder.newBuild()
                .addParam("id", rbacRoleBO.getId());

        RbacRoleBO respRbacRoleBO = this.rbacRoleService.get(paramsBuilder.build());
        if(respRbacRoleBO == null){
            throw new MallException("角色数据异常");
        }

        if (ReqCode.UPDATE_STATUS.equals(sign.getReqCode())){
            this.updateStatus(rbacRoleBO);
        }else {
            this.rbacRoleService.update(rbacRoleBO);
            RbacRolePermBO rbacRolePermBO = new RbacRolePermBO();
            rbacRolePermBO.setRoleId(rbacRoleBO.getId());
            this.rbacRolePermService.del(rbacRolePermBO);
//            rbacRoleBO.getPermIdList().forEach(permId -> {
//                RbacRolePermBO addRbacRolePermBO = new RbacRolePermBO();
//                addRbacRolePermBO.setRoleId(rbacRoleBO.getId());
//                addRbacRolePermBO.setPermId(permId);
//                this.rbacRolePermService.post(addRbacRolePermBO);
//            });
        }
        respRbacRoleBO.setName(rbacRoleBO.getName());
        respRbacRoleBO.setStatus(rbacRoleBO.getStatus());
        respSign.setSource(super.getSource(respRbacRoleBO));
        return respSign;
    }

    private void updateStatus(RbacRoleBO rbacRoleBO){
        RbacRoleBO updateRbacRoleBO = new RbacRoleBO();
        updateRbacRoleBO.setId(rbacRoleBO.getId());
        updateRbacRoleBO.setStatus(rbacRoleBO.getStatus());
        this.rbacRoleService.update(updateRbacRoleBO);
    }


    @Override
    public String getCmdCode() {
        return RbacCmdCode.RBAC_ROLE_PATCH;
    }

    @Autowired
    private IRbacRoleService rbacRoleService;
    @Autowired
    private IRbacRolePermService rbacRolePermService;
}
